function getParentSite( childNode ) {
    var siteType = "st:site";
    if ( childNode == null || childNode.typeShort == siteType ) {
          return childNode;
    } else {
          return getParentSite( childNode.parent );
    }
}

function getTemplate( siteNode, templateName ){
    var templatesSpaceName="templates";
    return siteNode.childByNamePath(templatesSpaceName).childByNamePath( templateName );
}

function getSiteTemplate( reportNode, templateName ) {
    var siteNode = getParentSite(reportNode);
    return getTemplate(siteNode, templateName);
}

function getPropertyValue(propValue){
     return propValue==null||propValue==""?"-":propValue.toString();
}
function getFormattedPropertyValue(propValue){
	var unformattedValue=propValue==null||propValue==""?"-":propValue.toString();
	var elements=unformattedValue.split(",");
	var formattedValue="";
	for (var element in elements) {
		var toFormatPos=elements[element].indexOf(" - ");
		var elementPart=elements[element].substring(toFormatPos+3);
		elementPart=formattedValue==""?elementPart:"<br/>"+elementPart;
		formattedValue=formattedValue+elementPart;
	}
	return formattedValue;
}
function replaceProperties( templateContent, node) {
     for (var prop in node.properties){
          	var indexPos=templateContent.indexOf("{"+prop+"}");
		var indexPos2=templateContent.indexOf("{{"+prop+"}}");
		if(indexPos!=-1||indexPos2!=-1) {
			if(indexPos2!=-1)
				templateContent=templateContent.replace(("{{"+prop+"}}").toString(),getFormattedPropertyValue(node.properties[prop]));
			else 
				templateContent=templateContent.replace(("{"+prop+"}").toString(),getPropertyValue(node.properties[prop]));
		}
     }
     return templateContent; 
}
function fixNonExistingAssociations(templateContent) {
	var assocTag="{{swr.model}solutionArchitectureDocument}";
	var indexPos=templateContent.indexOf(assocTag);
	if(indexPos!=-1){
		templateContent=templateContent.replace(assocTag,"-");
	}
	return templateContent; 
}
function replaceAssociations(templateContent, node) {
	for(assocs in node.assocs) {
		var indexPos=templateContent.indexOf("{"+assocs+"}");
		if(indexPos!=-1){
			var assocText="";
			for(assoc in node.assocs[assocs]) {
				assocText+="<a href='/share/proxy/alfresco/api/node/content/"+node.assocs[assocs][assoc].nodeRef.toString().replace(":/","")+"/"+node.assocs[assocs][assoc].name+"?a=true' target='_blank'>"+node.assocs[assocs][assoc].name+"</a><br/>";
			}
			templateContent=templateContent.replace(("{"+assocs+"}").toString(),assocText);
		}
		
	}
        templateContent=fixNonExistingAssociations(templateContent);
	return templateContent; 
}
function main() {
if (args.nodeRef == undefined || args.nodeRef == "")
{
   status.code = 400;
   status.message = "Node ref has not been provided.";
   status.redirect = true;
}else{
    var reportNode=search.findNode(args.nodeRef);
    if(reportNode==null) {
         status.code = 404;
         status.message = "Node ref provided does not exist";
         status.redirect = true;
    }else{
	//it could be added here the chance for passing the name of the template as an argument
	//right now it fetches from the type of the node to be reported (default behaviour to keep)
        var templateName = reportNode.typeShort.replace(":","_") + ".html";
        var templateNode=getSiteTemplate(reportNode, templateName);
	var templateContent=replaceAssociations(templateNode.content,reportNode);
        model.result=replaceProperties(templateContent,reportNode);
    }
}
}

main();
